﻿/// <reference path="jquery/jquery-1.4.1-vsdoc.js" />

String.prototype.getFileExt = function() {
    return (/[.]/.exec(this))[0] ? /[^.]+$/.exec(this)[0].toLowerCase() : undefined;
}

String.prototype.splitAlphabetAndOthers = function() {
    var reg = /([^\x00-\xff])/g;
    return $.trim(this.replace(reg, " $1 ").replace(/\s+/g, ' ')).split(' ');
}

var prm;
var avl; // async loading message
var om; // operation message
var omwrapper;
var avl1;
var avlTimeout;

$(function () {
    var flashVars = {};
    var params = { wmode: "transparent" };
    var attr = {};
    if ($("homeami").length > 0) {
        swfobject.embedSWF("/Content/images/500x8020100813.swf", "homeami", 400, 64, "9.0.0", "", flashVars, params, attr);
    }
});

$(function () {
    
    $.ajaxSetup(
    {
        beforeSend: function (xhr) {
            //CallOM("正在加载...", true);
        },
        complete: function (xhr, status) {
            DelayHideOM();
            InitialButtons();
            $('.fd').cuteTime();
        },
        error: function () {
            DelayHideOM();
        }
    });



    $('.fd').cuteTime();

    jQuery.namespace = function () {
        var a = arguments, o = null, i, j, d;
        for (i = 0; i < a.length; i = i + 1) {
            d = a[i].split(".");
            o = window;
            for (j = 0; j < d.length; j = j + 1) {
                o[d[j]] = o[d[j]] || {};
                o = o[d[j]];
            }
        }
        return o;
    };

    jQuery.query = function () {
        var r = {};
        var q = location.search;
        q = q.replace(/^\?/, ''); // remove the leading ?         
        q = q.replace(/\&$/, ''); // remove the trailing & 
        jQuery.each(q.split('&'), function () {
            var key = this.split('=')[0];
            var val = this.split('=')[1];
            // convert floats 
            if (/^[0-9.]+$/.test(val))
                val = parseFloat(val);
            // ingnore empty values 
            if (val)
                r[key] = val;
        });
        return r;
    };
    //$('.jtab').tabs();
    //$(".datepicker").datepicker({ showAnim: '' });
    InitializeDatePicker();
    avl = $('span[title=avl]');

    //avl = $find('av-loading');
    InitialButtons();
    prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_beginRequest(
        function (sender, args) {
            avl.text("正在加载...");
            avl.show();
        }
    );
    prm.add_endRequest(
    function (sender, args) {
        InitializeDatePicker();
        InitialButtons();
        if (args.get_error() == null) {
            avl.hide();
        }
        else {
            CallOM(args.get_error().message);
        }

        $('.fd').cuteTime();
    }
    );

    if (avl.hasClass('showonload')) {
        avl.show();
        DelayHideOM();
    }
    else {
        avl.hide();
    }
});

function InitializeDatePicker() {

    var dates = $('.startdate, .enddate').datepicker({
        showAnim: '',
        //defaultDate: $(this).val() == "" ? "+1w" : new Date($(this).val()),
        changeMonth: false,
        numberOfMonths: 2,
        gotoCurrent: true,
        minDate: new Date(),
        dateFormat: 'yy-mm-dd',
        onSelect: function(selectedDate) {
            var option = $(this).hasClass("startdate") ? "minDate" : "maxDate";
            var instance = $(this).data("datepicker");
            var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
            dates.not(this).datepicker("option", option, date);
            //            if ($(this).hasClass("startdate")) {
            //                $('.enddate').focus();
            //            }
        },
        beforeShow: function(input, instance) {
        var d = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, $(input).val(), instance.settings);
        $(this).datepicker('setDate', d);
        }

    });

        var offdate = $('.offdate').datepicker({
        showAnim: '',
        gotoCurrent: true,
        dateFormat: 'yy-mm-dd',
            onSelect: function(selectedDate) {
                var instance = $('.startdate').data("datepicker");
                var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
                if (instance.selectedYear == 0) {
                    alert("请先选择开始日期。");

                    $('.startdate').focus();
                }
                else {
                }
            }
        });
        
        
}

function DelayHideOM(ms) {
    //clearTimeout(avlTimeout);
    if (avl.text() != "") {
        avlTimeout = setTimeout
                (
                        function() {
                            avl.hide();
                        },
                        undefined == ms ? 3000 : ms
                );
    }
    else {
        avl.hide();
    }
}

function CallOM(text, alwaysShow) {
    clearTimeout(avlTimeout);
    avl.text(text);
    avl.show();
    try {
        $find('avlbhv')._reposition();
    } catch (e) { }
    if (!alwaysShow) {
        DelayHideOM();
    }
}

function InitialButtons() {
    $('.closemodalformbutton').button({
        icons: {
            primary: 'ui-icon-close'
        }
    });

    $('.openmodalformbutton').button({
        icons: {
            primary: 'ui-icon-extlink'
        }
    });

    $('.submitbutton').button({
        icons: {
            primary: 'ui-icon-check'
        }
    }
    );

    $('.addbutton').button({
        icons: {
            primary: 'ui-icon-plus'
        }
    }
    );

    $('.cancelbutton').button({
        icons: {
            primary: 'ui-icon-close'
        }
    });

    $('.deletebutton').button({
        icons: {
            primary: 'ui-icon-minus'
        }
    });

    $('.pager-f').button({
        icons: {
            primary: 'ui-icon-seek-first'
        }
    });

    $('.pager-l').button({
        icons: {
            primary: 'ui-icon-seek-end'
        }
    });

    $('.replybutton').button({
        icons: {
            primary: 'ui-icon-arrowreturnthick-1-e'
        }
    });

    $('.dropdownbutton').button({
    icons: {
            //primary:'nan',
            primary: 'ui-icon-carat-1-s'
        }
    });

    $('.pager-n').button();
    $('.button').button();



    var dlg = $(".dialogform").dialog({
        autoOpen: false,
        height: 'auto',
        width: 'auto',
        maxHeight: 700,
        maxWidth: 800,
        //zIndex: 900,
        modal: true,
        close: function(event, ui) {
            $('.ajax__validatorcallout_popup_table').css('visibility', 'hidden');
        },
        open: function(event, ui) {
            $(this).css('height', 'auto');
            var d = $(".dialogform");
            d.prev().css("width", d.width());
            //alert($(".dialogform").width());
            $(this).parent().appendTo(jQuery("form:first"));
        }
    });

    

    $('.closemodalformbutton').click
    (function() {
        $('.dialogform').dialog('close');
        return false;
    });

    $('.openmodalformbutton').click(function() {
        $('.dialogform').dialog('open');
        return false;
    });

    $('.openmodalformbutton-nostyle').click
    (function() {
    $('.dialogform').dialog('open');
        return false;
    });

    $('.closemodalformbutton-nostyle').click
    (function() {
        $('.dialogform').dialog('close');
        return false;
    });

    dlg.parent().appendTo(jQuery("form:first"));
    //OpenJqueryDialog();
    //CloseJqueryDialog();
}

function OpenJqueryDialog() {
    $('.dialogform').dialog('open');
}

function CloseJqueryDialog() {
    $('.dialogform').dialog('close');
}

function InsertDom(containerSel, domSel) {
    $(domSel).clone().appendTo(containerSel);
}





function GenericFavorite() {
    // bool
    this.AddFav = function(cName, cId, uId, cmt) {
        var ret = false;
        $.ajax({
            type: 'POST',
            url: relRootPath + '/Mod/Favorite/FavoriteServices.asmx/AddFavorite',
            data: ({
                contextName: cName,
                contextId: cId,
                userId: uId,
                comment: cmt
            }),
            async: false,
            success: function(data, status, x) {
                ret = eval($(data).text());
            }
        });
        return ret;
    }
    
    // bool
    this.DelFav = function(cName, cId, uId) {
        var ret = false;

        $.ajax({
            type: 'POST',
            url: relRootPath + '/Mod/Favorite/FavoriteServices.asmx/DeleteFavorite',
            data: {
                contextName: cName,
                contextId: cId,
                userId: uId
            },
            async: false,
            success: function(data, status, x) {
                ret = eval($(data).text());
            }
        });

        return ret;
    }
}

/*********** UTILITY ************/
Array.prototype.remove = function (from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};

function NewGuid() {
    var guid = "";
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += "-";
    }
    return guid;
}